-<template>
  <div class="app-container">
    <!-- 条件筛选组件 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="角色名称:">
        <el-input v-model.trim="query.name"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        <el-button
          v-if="!roleIds"
          icon="el-icon-circle-plus-outline"
          type="primary"
          @click="openAdd"
          >新增</el-button
        >
        <el-button
          v-if="roleIds"
          icon="el-icon-circle-plus-outline"
          type="success"
          @click="handleUserRole"
          >设置角色</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      ref="dataTable"
      row-key="id"
      :data="list"
      stripe
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <!-- reserve-selection必须配和row-key进行试用，这个是在切换页码后保留前面的选择 -->
      <el-table-column
        align="center"
        type="selection"
        width="55"
        reserve-selection
      ></el-table-column>
      <el-table-column align="center" label="序号" width="60" type="index">
      </el-table-column>
      <el-table-column align="center" prop="name" label="角色名称">
      </el-table-column>

      <el-table-column align="center" prop="remark" label="备注">
      </el-table-column>

      <el-table-column align="center" label="操作" v-if="!roleIds">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleDeletePermision(scope.row.id)"
            >分配权限</el-button
          >
          <el-button
            type="success"
            size="mini"
            @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>

    <edit
      :title="edit.title"
      :visible="edit.visible"
      :formData="edit.formData"
      :remoteClose="remoteClose"
    ></edit>
    <!-- 设置权限 -->
    <permission
      title="权限管理"
      :visible="per.visible"
      :remoteClose="remotePerClose"
      :roleId="per.roleId"
    />
  </div>
</template>

<script>
import api from "@/api/role.js";
import Edit from "./edit.vue";
import Permission from "./permission";
export default {
  // 当用户管理模块将这个组件当做子组件时传递来的值
  props: {
    roleIds: null,
  },
  data() {
    return {
      list: [], //列表数据
      page: {
        current: 1,
        size: 20,
        total: 0, //总记录数
      },
      query: {}, //查询条件
      edit: {
        title: "",
        visible: false,
        formData: {},
      },
      per: {
        visible: false,
        roleId: null,
      },
      checkedRoleList: [], //存储选中的角色
    };
  },
  created() {
    this.fetchData();
  },
  components: {
    Edit,
    Permission,
  },

  methods: {
    fetchData() {
      api.getList(this.query, this.page.current, this.page.size).then((res) => {
        this.list = res.data.records;
        this.page.total = res.data.total;
        //列表有数据后，勾选角色
        this.checkedRoles();
      });
    },
    checkedRoles() {
      this.$refs.dataTable.clearSelection();
      if (this.roleIds) {
        this.list.forEach((item) => {
          //循环出查询到的每个角色数据，
          //要进行判断父组件中的roleIds中的每个角色对象
          // 匹配到了则应该选中
          if (this.roleIds.indexOf(item.id) !== -1) {
            //选中，对应的传递的是角色对象
            this.$refs.dataTable.toggleRowSelection(item, true);
          }
        });
      }
    },
    handleDeletePermision(id) {
      this.per.visible = true;
      this.per.roleId = id;
    },
    handleEdit(id) {
      api.getById(id).then((res) => {
        this.edit.formData = res.data;
        this.edit.title = "编辑";
        this.edit.visible = true;
      });
    },
    handleDelete(id) {
      this.$confirm("确认删除这条记录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //发送删除请求
          api.deleteById(id).then((res) => {
            this.$message({
              type: res.code == 20000 ? "success" : "error",
              message: res.message,
            });
          });
          this.fetchData();
        })
        .catch(() => {});
    },
    // val是指每页显示多少条
    handleSizeChange(val) {
      this.page.size = val;
      this.fetchData();
    },
    // 当页码改变后触发
    handleCurrentChange(val) {
      this.page.current = val;
      this.fetchData();
    },
    queryData() {
      // 页码设置为1
      this.page.current = 1;
      this.fetchData();
    },
    openAdd() {
      this.edit.visible = true;
      this.edit.title = "新增";
    },
    remoteClose() {
      this.edit.formData = {};
      this.edit.visible = false;
      this.fetchData();
    },
    reload() {
      this.query = {};
      this.fetchData();
    },
    remotePerClose() {
      this.per.visible = false;
      this.per.roleId = null;
      this.fetchData9;
    },
    //收集被选中的角色
    handleSelectionChange(val) {
      this.checkedRoleList = val;
    },
    handleUserRole() {
      // 获取每个元素中的角色id，因为调用保存用户角色接口，
      // 只需要选中的角色id
      const checkedRoleIds = []; //存储选中的角色id
      this.checkedRoleList.forEach((item) => {
        checkedRoleIds.push(item.id);
      });
      //触发父组件的事件函数
      this.$emit("saveUserRole", checkedRoleIds);
    },
  },
  watch: {
    roleIds() {
      //当角色里的内容变化就会进行重新查询
      this.query = {};
      this.queryData();
    },
  },
};
</script>

<style scoped>
</style>